package com.sangcomz.fishbun.ui.detail.adapter;

import com.sangcomz.fishbun.ResourceTable;
import com.sangcomz.fishbun.adapter.image.ImageAdapter;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;
import ohos.utils.net.Uri;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sangcomz on 15/06/2017.
 */

public class DetailViewPagerAdapter extends PageSliderProvider {
    private ImageAdapter imageAdapter;
    private List<Uri> images = new ArrayList<>();
    private AbilitySlice abilitySlice;

    public DetailViewPagerAdapter(ImageAdapter imageAdapter, AbilitySlice abilitySlice) {
        this.imageAdapter = imageAdapter;
        this.abilitySlice = abilitySlice;
    }

    @Override
    public int getCount() {
        return images.size();
    }

    @Override
    public Object createPageInContainer(ComponentContainer componentContainer, int position) {
        Component itemView =
                LayoutScatter.getInstance(componentContainer.getContext())
                        .parse(ResourceTable.Layout_detail_item, componentContainer, false);
        componentContainer.addComponent(itemView);
        Image touchImageView = (Image) itemView.findComponentById(ResourceTable.Id_img_detail_image);
        imageAdapter.loadDetailImage(abilitySlice, touchImageView, images.get(position));
        return itemView;
    }

    @Override
    public void destroyPageFromContainer(ComponentContainer componentContainer, int position, Object object) {
        componentContainer.removeComponent((Component) object);
    }

    @Override
    public boolean isPageMatchToObject(Component component, Object obj) {
        return component == obj;
    }

    public void setImages(List<Uri> images) {
        System.out.println("===setImages===>" + images.toString());
        this.images = images;
        notifyDataChanged();
    }
}